<template>
  <div id="edit-blog">
    <h2>编辑博客</h2>
    <form v-if="!submmited">
      <label>博客标题</label>
      <input type="text" v-model="blog.title" required/>
      <label>博客内容</label>
      <textarea v-model="blog.content"></textarea>
      <label>博客分类</label>
      <div id="checkboxes">
        <label>Vue.js</label>
        <input type="checkbox" value="Vue.js" v-model="blog.categories" v-on:change="fn()" />
        <label>Node.js</label>
        <input type="checkbox" value="Node.js" v-model="blog.categories" v-on:change="fn()" />
        <label>React.js</label>
        <input type="checkbox" value="React.js" v-model="blog.categories" v-on:change="fn()" />
        <label>Angular.js</label>
        <input type="checkbox" value="Angular.js" v-model="blog.categories" v-on:change="fn()" />
      </div>
      <label>博客作者</label>
      <select v-model="blog.author">
        <option v-for="author in authors" :key="author">{{author}}</option>
      </select>
      <button v-on:click.prevent="post">编辑博客</button>
    </form>
    <hr>
    <div v-if="submmited">
      <h3>您的博客发送成功</h3>
    </div>
    <div id="preview">
      <h3>博客总览</h3>
      <p>博客标题：{{blog.title}}</p>
      <p>博客内容：</p>
      <p>{{blog.content}}</p>
      <p>博客分类：</p>
      <ul>
        <li v-for="category in blog.categories" :key="category">{{category}}</li>

      </ul>
      <p>作者：{{blog.author}}</p>
    </div>
  </div>
</template>

<script>
export default {
  name: "EditBlog",
  data() {
    return {
      id: this.$route.params.id,
      blog: {

      },
      authors: ["小红", "小米", "小明"],
      submmited: false
    };
  },
  methods: {
    fn() {
      console.log(this.blog.categories);
    },

    post: function() {
      this.$http
        .put("https://vuedemo-8dc9d.firebaseio.com/posts/" + this.id + ".json", this.blog)
        .then(function(data) {
          console.log(data);
          this.submmited = true;
        });
    },
    fetchData() {
      this.$http.get(
        "https://vuedemo-8dc9d.firebaseio.com/posts/" + this.id + ".json"
      ).then(res=>{
        console.log(res.body)
        this.blog=res.body;
      })
    }
    /* post: function() {
      this.$http
        .post("http://jsonplaceholder.typicode.com/posts", {
          title: this.blog.title,
          body: this.blog.content,
          userId: 1
        })
        .then(function(data) {
          console.log(data);
          this.submmited = true;
        });
    } */
  },
  created() {
    this.fetchData();
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#edit-blog * {
  box-sizing: border-box;
}
#edit-blog {
  margin: 20px auto;
  max-width: 600px;
  padding: 20px;
}
label {
  display: block;
  margin: 20px 0 10px;
}
input[type="text"],
textarea,
select {
  display: block;
  width: 100%;
  padding: 8px;
}
textarea {
  height: 200px;
}
#checkboxes label {
  display: inline;
  margin-top: 0px;
}
#checkboxes input {
  display: inline;
  margin-right: 10px;
}
button {
  display: block;
  margin: 20px 0;
  background: crimson;
  color: #fff;
  border: 0;
  padding: 14px;
  border-radius: 4px;
  font-size: 18px;
  cursor: pointer;
}
#preview {
  padding: 10px 20px;
  border: 1px dotted #cccccc;
  margin: 30px 0;
}
h3 {
  margin-top: 10px;
}
</style>
